using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITS.DAL.Partners;
using ITS.Core.Domain.Partners;

namespace ITS.DAL.Concrete.EF.Partners
{
    public class EFPartnerRepository : IPartnerRepository
    {
        private readonly EnterpriseContext context = new EnterpriseContext();

        public IQueryable<Partner> GetAllPartners()
        {
            return context.Partners;
        }

        public IQueryable<Partner> GetAllActivePartners()
        {
            return context.Partners.Where(x => x.Active == true);
        }

        public IQueryable<Partner> GetActivePartnersByCategory(int CategoryID)
        {
            return context.Partners.Where(x => x.CategoryID == CategoryID && x.Active == true);
        }

        public IQueryable<PartnerCategory> GetActivePartnerCategories()
        {
            return context.PartnerCategories.Where(x => x.Active == true);
        }

        public Partner Get(Guid partnerID)
        {
            return context.Partners.FirstOrDefault(x => x.PartnerID == partnerID);
        }

        public void Save(Partner o)
        {
            if (o.PartnerID.Equals(Guid.Empty))
            {
                o.PartnerID = Guid.NewGuid();
                context.Partners.Add(o);
            }
            else
            {
                context.Entry(o).State = System.Data.EntityState.Modified;
            }
            context.SaveChanges();
        }
    }
}
